/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _DAL_FASTDDS_DAL_DATAWRITER_IMPL_HPP_
#define _DAL_FASTDDS_DAL_DATAWRITER_IMPL_HPP_

#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include "dal_fastdds/listener_impl/DataWriterListenerImpl.hpp"
#include "dal_fastrtps_cpp/type_support_common.hpp"

#ifdef DAL_ENABLE_DMABUFHEAP
#include "dal_fastdds/dbh/DalDbhWriter.hpp"
#endif

using namespace eprosima::fastdds::dds;

class DalDataWriterImpl
{
public:
enum class WriterType
{
    DDS_WRITER,
    DBH_WRITER,
    MIX_WRITER
};

public:
    explicit DalDataWriterImpl(DataWriter* fastdds_datawriter, DataWriterListenerImpl* listener);
    ~DalDataWriterImpl();

    DataWriter*& FastddsDataWriter();
    void SetFastddsDataWriter(DataWriter* fastdds_datawriter);
    Publisher*& FastddsPublisher();
    void SetFastddsPublisher(Publisher*& publisher);
    DomainParticipant*& FastddsDomainParticipant();
    void SetFastddsDomainParticipant(DomainParticipant*& participant);
    void SetTypeSupport(const void* type_support);
    ReturnCode_t DDSWrite(void* data, const InstanceHandle_t& instance_handle);
#ifdef DAL_ENABLE_DMABUFHEAP
    ReturnCode_t DBHWrite(void* data, const InstanceHandle_t& instance_handle);
    
    // used for dmabufheap transfer
    DalDbhWriter* dbh_writer_ = nullptr;
#endif
    WriterType type_ = WriterType::DDS_WRITER;

private:
    DataWriterListenerImpl* listener_ = nullptr;
    DataWriter* fastdds_datawriter_ = nullptr;
    Publisher* fastdds_pub_ = nullptr;
    DomainParticipant* fastdds_participant_ = nullptr;
    const void* type_support_ = nullptr;
};

#endif  // _DAL_FASTDDS_DAL_DATAWRITER_IMPL_HPP_
